package com.yiqi.zhiyuan.feature.mine.presenter;

import android.text.TextUtils;

import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.ValidatorUtil;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.dto.FeedbackDTO;
import com.yiqi.zhiyuan.entity.vo.FileVO;
import com.yiqi.zhiyuan.feature.mine.contract.FeedbackContract;
import com.yiqi.zhiyuan.feature.mine.dependencies.feedback.DaggerFeedbackPresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @ProjectName: ZhiYuan
 * @Description: java类作用描述
 * @Author: lijuwen
 * @CreateDate: 2019/3/14 10:54
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/14 10:54
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class FeedbackPresenter implements FeedbackContract.Presenter {

    private FeedbackContract.View mView;

    @Inject
    FeedbackContract.Model mModel;


    public FeedbackPresenter(FeedbackContract.View mView) {
        this.mView = mView;
        DaggerFeedbackPresenterComponent.create().inject(this);
    }


    @Override
    public void start() {

    }

    @Override
    public void uploadImage(String imageUrl) {
        if (TextUtils.isEmpty(imageUrl)) return;
        mModel.doUploadImage(imageUrl)
                .compose(mView.bindToLife())
                .subscribe(new Observer<FileVO>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FileVO fileVO) {
                        if (fileVO != null) {
                            mView.showUploadImageSuccess(fileVO.getImgUrl());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showUploadImageError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void doFeedback(String content, String phone, List<String> imgs) {
        if (TextUtils.isEmpty(content)) {
            mView.showFeedbackError("请输入反馈内容");
            return;
        }
        if (!TextUtils.isEmpty(phone)) {
            if (!ValidatorUtil.isMobile(phone)) {
                mView.showFeedbackError(AM.res().getString(R.string.toast_phone_error));
                return;
            }
        }

        FeedbackDTO feedbackDTO = new FeedbackDTO();
        feedbackDTO.setContent(content);
        feedbackDTO.setImgList(imgs);
        feedbackDTO.setPhone(TextUtils.isEmpty(phone)?null:phone);
        mModel.doFeedback(feedbackDTO)
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String fileVO) {
                        mView.showFeedbackSuccess();
                        mView.dismissLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showFeedbackError(e.getMessage());
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        if (TextUtils.isEmpty(avatarUrl)) return;
        mModel.doUpdateAvatar(avatarUrl)
                .compose(mView.bindToLife())
                .subscribe(new Observer<FileVO>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                           mView.showLoading(d);
                    }

                    @Override
                    public void onNext(FileVO fileVO) {
                        mView.dismissLoading();
                        if (fileVO != null) {
                            mView.showUploadImageSuccess(fileVO.getImgUrl());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showUploadImageError(e.getMessage());
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }
}
